package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.MyThreadLocal;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Map;


@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private UserApi userApi;



    //查询用户通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();

        Long userId = MyThreadLocal.getUserId();
        String userMobile = MyThreadLocal.getUserMobile();
        vo.setId(userId);
        vo.setPhone(userMobile);

        Question question = questionApi.getById(userId);

        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);

        Settings settings = settingsApi.getById(userId);
        if (settings != null) {
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }

        return vo;
    }

    //设置陌生人问题
    public void saveQuestion(String content) {

        Long userId = MyThreadLocal.getUserId();
        Question question = questionApi.getById(userId);
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //3.2 如果存在，更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    //设置通知
    public void saveNotifications(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        Long userId = MyThreadLocal.getUserId();

        Settings settings = settingsApi.getById(userId);
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        } else {
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }

    //分页查询黑名单列表
    public PageResult blacklist(int page, int pagesize) {
        Long userId = MyThreadLocal.getUserId();
        //调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> iPage = blackListApi.getById(userId, page, pagesize);
        //对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page, pagesize, (int) iPage.getTotal(), iPage.getRecords());

        return pr;
    }

    //取消黑名单
    public void deleteBlackList(String uid) {
        Long userId = MyThreadLocal.getUserId();

        settingsApi.delete(userId,uid);
    }

    //修改手机号  验证码
    public void sendMsg(String phone) {
        String code = "123456";
        redisTemplate.opsForValue().set("UPDATE_CHECK_CODE_" + phone, code, Duration.ofMinutes(5));
    }


    //修改手机号 校验验证码
    public Boolean checkVerificationCode(String phone, String code) {
        String redisCode =redisTemplate.opsForValue().get("UPDATE_CHECK_CODE_" + phone);
        if (StringUtils.isEmpty(redisCode) || !code.equals(redisCode)) {
            throw new RuntimeException("验证码错误");
        }
        return true;
    }

    //修改手机号 保存
    public void updatePhone(String newPhone) {
        String mobile = MyThreadLocal.getUserMobile();
        User user = userApi.findByMobile(mobile);
        user.setMobile(newPhone);
        userApi.update(user);
    }
}
